from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets, mixins
from rest_framework.decorators import api_view

from rest_framework.response import Response

from ares.models import Area
from ares.serializer import AreasSerializer


class Areasview(viewsets.GenericViewSet,
           mixins.RetrieveModelMixin,
            mixins.ListModelMixin,
           # GET  /api/article/[id]/
            ):
    queryset = Area.objects.all()
    serializer_class = AreasSerializer

    def retrieve(self, request, *args, **kwargs):
        cities=self.get_queryset().filter(parent_id=kwargs['pk'])
        serializer = self.get_serializer(cities, many=True)
        print('23333')
        res = {'data': {'subs': serializer.data}}
        return Response(res)


    def list(self, request, *args, **kwargs):
        areas = Area.objects.filter(parent_id=None)
        provinces = []
        for area in areas:
            dict = {}
            dict['id'] = area.id
            dict['name'] = area.name
            provinces.append(dict)
        res = {'data': provinces}
        return Response(res)